package org.jeecg.modules.wms.warehouse.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.warehouse.entity.WmsStorageZones;
import org.jeecg.modules.wms.warehouse.mapper.WmsStorageZonesMapper;
import org.jeecg.modules.wms.warehouse.service.IWmsStorageZonesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 储区表
 * @Author: jeecg-boot
 * @Date:   2025-09-03
 * @Version: V1.0
 */
@Service
public class WmsStorageZonesServiceImpl extends ServiceImpl<WmsStorageZonesMapper, WmsStorageZones> implements IWmsStorageZonesService {
    @Autowired
    private WmsStorageZonesMapper wmsStorageZonesMapper;
    @Override
    public IPage<WmsStorageZones> queryList(WmsStorageZones wmsStorageZones, Integer pageNo, Integer pageSize) {
        Page<WmsStorageZones> page = PageHelper.startPage(pageNo, pageSize);
        List<WmsStorageZones> list = wmsStorageZonesMapper.queryList(wmsStorageZones);
        PageDTO<WmsStorageZones> pageVo = new PageDTO<>();
        pageVo.setRecords(list);
        pageVo.setTotal(page.getTotal());//总数
        pageVo.setPages(page.getPages());//总页数
        pageVo.setCurrent(pageNo);//当前页
        pageVo.setSize(pageSize);//每页数
        return pageVo;

    }

    @Override
    public void enable(String id) {
        //校验参数是否为空
        if(StringUtils.isEmpty(id)){
            throw new JeecgBootException("参数为空");
        }
        //根据id查找判断储区是否存在
        WmsStorageZones wmsStorageZones = getById(id);
        if(wmsStorageZones == null){
            throw new JeecgBootException("储区不存在");
        }
        //只有状态为禁用的储区才能够使用
        if (!WarehouseDictEnum.STATUS_INACTIVE.getCode().equals(wmsStorageZones.getStatus())){
            throw new JeecgBootException("储区状态为“禁用”时方可启用");
        }
        wmsStorageZones.setStatus(WarehouseDictEnum.STATUS_ACTIVE.getCode());
        updateById(wmsStorageZones);
    }

    @Override
    public void disable(String id) {
        //校验参数是否为空
        if(StringUtils.isEmpty(id)){
            throw new JeecgBootException("参数为空");
        }
        //根据id查找判断储区是否存在
        WmsStorageZones wmsStorageZones = getById(id);
        if(wmsStorageZones == null){
            throw new JeecgBootException("储区不存在");
        }
        //只有状态为创建和启用的储区才能够禁用
        if (!WarehouseDictEnum.STATUS_CREATED.getCode().equals(wmsStorageZones.getStatus())
        && !WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(wmsStorageZones.getStatus())){
            throw new JeecgBootException("储区状态为“创建”或“启用”时方可禁用");
        }
        wmsStorageZones.setStatus(WarehouseDictEnum.STATUS_INACTIVE.getCode());
        updateById(wmsStorageZones);

    }
}
